home *** CD-ROM | disk | FTP | other *** search
/ Programming an RTS Game with Direct3D / Programming an RTS Game with Direct3D.iso / Examples / Chapter 5 / Example 5.10 / app.cpp next >
Encoding:
C/C++ Source or Header  |  2006-08-01  |  7.1 KB  |  280 lines

  1. //////////////////////////////////////////////////////////////
  2. // Example 5.10: Lighting the Terrain                        //
  3. // Written by: C. Granberg, 2006                            //
  4. //////////////////////////////////////////////////////////////
  5.  
  6. #include <windows.h>
  7. #include <d3dx9.h>
  8. #include "debug.h"
  9. #include "heightMap.h"
  10. #include "terrain.h"
  11. #include "camera.h"
  12. #include "mouse.h"
  13.  
  14. class APPLICATION
  15. {
  16.     public:
  17.         APPLICATION();
  18.         HRESULT Init(HINSTANCE hInstance, int width, int height, bool windowed);
  19.         HRESULT Update(float deltaTime);
  20.         HRESULT Render();
  21.         HRESULT Cleanup();
  22.         HRESULT Quit();
  23.         DWORD FtoDword(float f){return *((DWORD*)&f);}
  24.  
  25.     private:
  26.         IDirect3DDevice9* m_pDevice; 
  27.         TERRAIN m_terrain;
  28.         CAMERA m_camera;
  29.         MOUSE m_mouse;
  30.  
  31.         bool m_wireframe;
  32.         DWORD m_time, m_snapTime;
  33.         int m_fps, m_lastFps;
  34.         HWND m_mainWindow;
  35.         ID3DXFont *m_pFont;
  36. };
  37.  
  38. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, PSTR cmdLine, int showCmd)
  39. {
  40.     APPLICATION app;
  41.  
  42.     if(FAILED(app.Init(hInstance, 800, 600, true)))return 0;
  43.  
  44.     MSG msg;
  45.     memset(&msg, 0, sizeof(MSG));
  46.     int startTime = timeGetTime(); 
  47.  
  48.     while(msg.message != WM_QUIT)
  49.     {
  50.         if(::PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
  51.         {
  52.             ::TranslateMessage(&msg);
  53.             ::DispatchMessage(&msg);
  54.         }
  55.         else
  56.         {    
  57.             int t = timeGetTime();
  58.             float deltaTime = (t - startTime)*0.001f;
  59.  
  60.             app.Update(deltaTime);
  61.             app.Render();
  62.  
  63.             startTime = t;
  64.         }
  65.     }
  66.  
  67.     app.Cleanup();
  68.  
  69.     return msg.wParam;
  70. }
  71.  
  72. APPLICATION::APPLICATION()
  73. {
  74.     m_pDevice = NULL; 
  75.     m_mainWindow = 0;
  76.     m_wireframe = false;
  77.     srand(GetTickCount());
  78.     m_fps = m_lastFps = 0;
  79.     m_time = m_snapTime = GetTickCount();
  80. }
  81.  
  82. HRESULT APPLICATION::Init(HINSTANCE hInstance, int width, int height, bool windowed)
  83. {
  84.     debug.Print("Application initiated");
  85.  
  86.     //Create Window Class
  87.     WNDCLASS wc;
  88.     memset(&wc, 0, sizeof(WNDCLASS));
  89.     wc.style         = CS_HREDRAW | CS_VREDRAW;
  90.     wc.lpfnWndProc   = (WNDPROC)::DefWindowProc; 
  91.     wc.hInstance     = hInstance;
  92.     wc.lpszClassName = "D3DWND";
  93.  
  94.     //Register Class and Create new Window
  95.     RegisterClass(&wc);
  96.     m_mainWindow = CreateWindow("D3DWND", "Example 5.10: Lighting the Terrain", WS_EX_TOPMOST, 0, 0, width, height, 0, 0, hInstance, 0); 
  97.     SetCursor(NULL);
  98.     ShowWindow(m_mainWindow, SW_SHOW);
  99.     UpdateWindow(m_mainWindow);
  100.  
  101.     //Create IDirect3D9 Interface
  102.     IDirect3D9* d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
  103.  
  104.     if(d3d9 == NULL)
  105.     {
  106.         debug.Print("Direct3DCreate9() - FAILED");
  107.         return E_FAIL;
  108.     }
  109.  
  110.     //Check that the Device supports what we need from it
  111.     D3DCAPS9 caps;
  112.     d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);
  113.  
  114.     //Hardware Vertex Processing or not?
  115.     int vp = 0;
  116.     if(caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
  117.         vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
  118.     else vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
  119.  
  120.     //Check vertex & pixelshader versions
  121.     if(caps.VertexShaderVersion < D3DVS_VERSION(2, 0) || caps.PixelShaderVersion < D3DPS_VERSION(2, 0))
  122.     {
  123.         debug.Print("Warning - Your graphic card does not support vertex and pixelshaders version 2.0");
  124.     }
  125.  
  126.     //Set D3DPRESENT_PARAMETERS
  127.     D3DPRESENT_PARAMETERS d3dpp;
  128.     d3dpp.BackBufferWidth            = width;
  129.     d3dpp.BackBufferHeight           = height;
  130.     d3dpp.BackBufferFormat           = D3DFMT_A8R8G8B8;
  131.     d3dpp.BackBufferCount            = 1;
  132.     d3dpp.MultiSampleType            = D3DMULTISAMPLE_NONE;
  133.     d3dpp.MultiSampleQuality         = 0;
  134.     d3dpp.SwapEffect                 = D3DSWAPEFFECT_DISCARD; 
  135.     d3dpp.hDeviceWindow              = m_mainWindow;
  136.     d3dpp.Windowed                   = windowed;
  137.     d3dpp.EnableAutoDepthStencil     = true; 
  138.     d3dpp.AutoDepthStencilFormat     = D3DFMT_D24S8;
  139.     d3dpp.Flags                      = 0;
  140.     d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
  141.     d3dpp.PresentationInterval       = D3DPRESENT_INTERVAL_IMMEDIATE;
  142.  
  143.     //Create the IDirect3DDevice9
  144.     if(FAILED(d3d9->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_mainWindow,
  145.                                  vp, &d3dpp, &m_pDevice)))
  146.     {
  147.         debug.Print("Failed to create IDirect3DDevice9");
  148.         return E_FAIL;
  149.     }
  150.  
  151.     //Release IDirect3D9 interface
  152.     d3d9->Release();
  153.  
  154.     D3DXCreateFont(m_pDevice, 18, 0, 0, 1, false,  
  155.                    DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY,
  156.                    DEFAULT_PITCH | FF_DONTCARE, "Arial", &m_pFont);
  157.  
  158.     LoadObjectResources(m_pDevice);
  159.     m_terrain.Init(m_pDevice, INTPOINT(100,100));
  160.  
  161.     m_mouse.InitMouse(m_pDevice, m_mainWindow);
  162.  
  163.     m_camera.Init(m_pDevice);
  164.     m_camera.m_focus = D3DXVECTOR3(50, 10, -50);
  165.     m_camera.m_fov = 0.6f;
  166.     m_camera.m_radius = 50.0f;
  167.  
  168.     //Set sampler state
  169.     for(int i=0;i<8;i++)
  170.     {
  171.         m_pDevice->SetSamplerState(i, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
  172.         m_pDevice->SetSamplerState(i, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
  173.         m_pDevice->SetSamplerState(i, D3DSAMP_MIPFILTER, D3DTEXF_POINT);
  174.     }
  175.  
  176.     return S_OK;
  177. }
  178.  
  179. HRESULT APPLICATION::Update(float deltaTime)
  180. {
  181.     //Control camera
  182.     m_camera.Update(m_mouse, m_terrain, deltaTime);
  183.     m_mouse.Update(m_terrain);
  184.     
  185.     if(KEYDOWN('W'))
  186.     {
  187.         m_wireframe = !m_wireframe;
  188.         Sleep(300);
  189.     }
  190.     else if(KEYDOWN(VK_SPACE))
  191.     {        
  192.         m_terrain.GenerateRandomTerrain(9);
  193.     }
  194.     else if(KEYDOWN(VK_ESCAPE))
  195.     {
  196.         Quit();
  197.     }
  198.     else if(m_mouse.ClickRight())
  199.     {
  200.         if(GetTickCount() - m_snapTime > 300)
  201.         {
  202.             m_snapTime = GetTickCount();
  203.             m_camera.m_focus = m_terrain.GetWorldPos(m_mouse.m_mappos);
  204.         }
  205.     }
  206.  
  207.     return S_OK;
  208. }    
  209.  
  210. HRESULT APPLICATION::Render()
  211. {
  212.     // Clear the viewport
  213.     m_pDevice->Clear( 0L, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0L );
  214.  
  215.     //FPS Calculation
  216.     m_fps++;
  217.     if(GetTickCount() - m_time > 1000)
  218.     {
  219.         m_lastFps = m_fps;
  220.         m_fps = 0;
  221.         m_time = GetTickCount();
  222.     }
  223.  
  224.     // Begin the scene 
  225.     if(SUCCEEDED(m_pDevice->BeginScene()))
  226.     {
  227.         if(m_wireframe)m_pDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);    
  228.         else m_pDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
  229.  
  230.         m_terrain.Render(m_camera);
  231.         m_mouse.Paint();
  232.  
  233.         //Mouse mappos
  234.         char number[50];
  235.         std::string text = "Mouse Mappos: ";
  236.         text += _itoa(m_mouse.m_mappos.x, number, 10);
  237.         text += ", ";
  238.         text += _itoa(m_mouse.m_mappos.y, number, 10);
  239.  
  240.         RECT r[] = {{10, 10, 0, 0}, {10, 50, 0, 0}, {10, 70, 0, 0}, {600, 10, 0, 0}};
  241.         m_pFont->DrawText(NULL, text.c_str(), -1, &r[0], DT_LEFT| DT_TOP | DT_NOCLIP, 0xff000000);
  242.         m_pFont->DrawText(NULL, "Space: Randomize Terrain", -1, &r[1], DT_LEFT| DT_TOP | DT_NOCLIP, 0xff000000);
  243.         m_pFont->DrawText(NULL, "Right MButton: Move Focus", -1, &r[2], DT_LEFT| DT_TOP | DT_NOCLIP, 0xff000000);
  244.  
  245.         //FPS
  246.         text = "FPS: ";
  247.         text += _itoa(m_lastFps, number, 10);
  248.         m_pFont->DrawText(NULL, text.c_str(), -1, &r[3], DT_LEFT| DT_TOP | DT_NOCLIP, 0xff000000);
  249.  
  250.         // End the scene.
  251.         m_pDevice->EndScene();
  252.         m_pDevice->Present(0, 0, 0, 0);
  253.     }
  254.  
  255.     return S_OK;
  256. }
  257.  
  258. HRESULT APPLICATION::Cleanup()
  259. {
  260.     try
  261.     {
  262.         m_terrain.Release();
  263.         UnloadObjectResources();
  264.  
  265.         m_pFont->Release();
  266.         m_pDevice->Release();
  267.  
  268.         debug.Print("Application terminated");
  269.     }
  270.     catch(...){}
  271.  
  272.     return S_OK;
  273. }
  274.  
  275. HRESULT APPLICATION::Quit()
  276. {
  277.     ::DestroyWindow(m_mainWindow);
  278.     ::PostQuitMessage(0);
  279.     return S_OK;
  280. }